package com.easy.generator.vue;

import com.easy.generator.Easy;
import com.easy.generator.annotation.RouterLabel;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.util.Set;

/**
 * @author easy
 * @date 2025年07月11日 18:09
 */
public class Index_View_Generator {
    private static final String DIR_NAME = Easy.VUE_LOCAL_PATH;
    private static final String FILE_INDEX_NAME = "Index.vue";
    private static final String PACKAGE_NAME = Easy.MODEL_PACKAGE + ".generator.vue.file";
    private static final String DEST_PATH = Easy.VUE_LOCAL_PATH + "/src/view";

    public static void generateIndex() throws Exception {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        // 扫描包下的所有类
        Set<Class<?>> classes = Easy.scanClasses(Easy.BEAN_PACKAGE, classLoader);
        // 为每个实体类生成Controller
        StringBuilder strb = new StringBuilder();

        strb.append("<script setup>\n");
        strb.append("import { reactive, ref } from 'vue';\n");
        strb.append("import router from '@/router';\n");
        strb.append("import loginApi from '@/api/login.js';\n");
        strb.append("import {adminStore, tokenStore} from '../store/store.js';\n");
        strb.append("\n");
        strb.append("const activeIndex = ref('1')\n");
        strb.append("\n");
        strb.append("// 取出当前登录用户的信息\n");
        strb.append("const loginBean = ref({\n");
        strb.append("  name: \"未登录\",\n");
        strb.append("});\n");
        strb.append("getLoginBean();\n");
        strb.append("\n");
        strb.append("const menuData = ref([\n");
        strb.append("  {name: '信息管理', icon: 'GobletFull', children: [\n");
        for (Class<?> clazz : classes) {
            if (clazz.isAnnotationPresent(RouterLabel.class)) {
                String label = clazz.getAnnotation(RouterLabel.class).label();
                String name = clazz.getSimpleName();
                strb.append("      {name: '" + label + "', icon: 'GobletSquareFull', path: \"/" + name.toLowerCase() + "\"},\n");
            }
        }
        strb.append("    ]},\n");
        strb.append("]);\n");
        strb.append("\n");
        strb.append("async function getLoginBean(){\n");
        strb.append("  const res = await loginApi.getloginbean();\n" );
        strb.append(       "  if (res) {\n" );
        strb.append(      "    loginBean.value = res;\n" );
        strb.append(      "    adminStore().update(res);\n" );
        strb.append(      "  } else {\n" );
        strb.append(      "    router.push('/login')\n" );
        strb.append(       "  }");
        strb.append("}\n");
        strb.append("// 用户下拉菜单的点击\n");
        strb.append("function handleSelect(key, keyPath){\n");
        strb.append("  if (key == 'logout'){\n");
        strb.append("    // 用户退出\n");
        strb.append("    logout();\n");
        strb.append("  }\n");
        strb.append("}\n");
        strb.append("// 左侧菜单动作\n");
        strb.append("function handleMenu(key){\n");
        strb.append("  if (key){\n");
        strb.append("    router.push(key);\n");
        strb.append("  }\n");
        strb.append("}\n");
        strb.append("// 退出\n");
        strb.append("function logout(){\n");
        strb.append("  // 清除Token\n");
        strb.append("   tokenStore().update(null);\n");
        strb.append("   adminStore().update(null);\n");
        strb.append("  // 跳转到登录\n");
        strb.append("  router.push('/login')\n");
        strb.append("}\n");
        strb.append("// 左侧菜单展开折叠\n");
        strb.append("const isCollapse = ref(false);\n");
        strb.append("</script>\n");
        strb.append("<template>\n");
        strb.append("  <div class=\"common-layout\">\n");
        strb.append("      <el-container>\n");
        strb.append("        <el-header>\n");
        strb.append("          <el-menu :default-active=\"activeIndex\"\n");
        strb.append("                   class=\"el-menu-demo\"\n");
        strb.append("                   mode=\"horizontal\"\n");
        strb.append("                   :ellipsis=\"false\"\n");
        strb.append("                   background-color=\"#f3f7ff\"\n");
        strb.append("                   text-color=\"#393939\"\n");
        strb.append("                   active-text-color=\"#ffd04b\"\n");
        strb.append("                   @select=\"handleSelect\">\n");
        strb.append("            <el-menu-item index=\"0\">\n");
        strb.append("              <img style=\"height: 80%\" src=\"@/assets/logo.png\" alt=\"易视界\" />\n");
        strb.append("              <span style=\"padding-left: 50px; font-size: 24px; color:#393939;\">"+Easy.PROJECR_NAME+"</span>\n");
        strb.append("            </el-menu-item>\n");
        strb.append("            <div class=\"flex-grow\" />\n");
        strb.append("            <el-sub-menu index=\"1\">\n");
        strb.append("              <template #title>\n");
        strb.append("                <el-icon><Stamp></Stamp></el-icon>\n");
        strb.append("                {{ loginBean?loginBean.name:'没登录' }}\n");
        strb.append("              </template>\n");
        strb.append("              <el-menu-item index=\"1-2\">重置密码</el-menu-item>\n");
        strb.append("              <el-menu-item index=\"logout\">退出\n");
        strb.append("                <el-icon><SwitchButton></SwitchButton></el-icon>\n");
        strb.append("              </el-menu-item>\n");
        strb.append("            </el-sub-menu>\n");
        strb.append("\n");
        strb.append("          </el-menu>\n");
        strb.append("        </el-header>\n");
        strb.append("        <el-container>\n");
        strb.append("          <el-aside :width=\"1\">\n");
        strb.append("            <div style=\"height: 60px;line-height: 60px; text-align: right; color: #FFF; font-size: 20px; margin-right: 24px;\">\n");
        strb.append("              <el-icon @click=\"isCollapse=!isCollapse\">\n");
        strb.append("                <Expand v-if=\"isCollapse\"></Expand>\n");
        strb.append("                <Fold v-else></Fold>\n");
        strb.append("              </el-icon>\n");
        strb.append("            </div>\n");
        strb.append("            <el-menu active-text-color=\"#ffd04b\" background-color=\"#0c1d40\" class=\"el-menu-vertical-demo\"\n");
        strb.append("                     default-active=\"/charts\" text-color=\"#fff\" :collapse=\"isCollapse\"\n");
        strb.append("                     @select=\"handleMenu\">\n");
        strb.append("              <!-- 动态生成菜单 -->\n");
        strb.append("              <el-sub-menu v-for=\"(menu, index) in menuData\" :index=\"index.toString()\">\n");
        strb.append("                <template #title>\n");
        strb.append("                  <component\n");
        strb.append("                      class=\"icons\"\n");
        strb.append("                      :is=\"menu.icon\"\n");
        strb.append("                      style=\"width: 1em; height: 1em; margin-right: 8px\" >\n");
        strb.append("                  </component>\n");
        strb.append("                  <span>{{ menu.name }}</span>\n");
        strb.append("                </template>\n");
        strb.append("                <el-menu-item v-for=\"(child, ind) in menu.children\" :index=\"child.path\">\n");
        strb.append("                  <el-icon><component\n");
        strb.append("                      :is=\"child.icon\">\n");
        strb.append("                  </component></el-icon>\n");
        strb.append("                  <span>{{ child.name }}</span>\n");
        strb.append("                </el-menu-item>\n");
        strb.append("              </el-sub-menu>\n");
        strb.append("\n");
        strb.append("            </el-menu>\n");
        strb.append("          </el-aside>\n");
        strb.append("        <el-main>\n");
        strb.append("          <router-view></router-view>\n");
        strb.append("        </el-main>\n");
        strb.append("      </el-container>\n");
        strb.append("    </el-container>\n");
        strb.append("  </div>\n");
        strb.append("</template>\n");
        strb.append("\n");
        strb.append("\n");
        strb.append("\n");
        strb.append("<style>\n");
        strb.append(".flex-grow {\n");
        strb.append("  flex-grow: 1;\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".common-layout,\n");
        strb.append(".el-container {\n");
        strb.append("  height: 100%;\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".el-header {\n");
        strb.append("  --el-header-padding: 0;\n");
        strb.append("  --el-header-height: 80px;\n");
        strb.append("  box-sizing: border-box;\n");
        strb.append("  flex-shrink: 0;\n");
        strb.append("  height: var(--el-header-height);\n");
        strb.append("  padding: var(--el-header-padding);\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".el-menu--horizontal {\n");
        strb.append("  height: 80px;\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".el-aside {\n");
        strb.append("  background-color: #0c1d40;\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".el-main{\n");
        strb.append("  padding: 0px;\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".el-menu{\n");
        strb.append("  border-width: 0px;\n");
        strb.append("}\n");
        strb.append(".el-menu-item{\n");
        strb.append("  font-size: 16px;\n");
        strb.append("}\n");
        strb.append(".el-menu-demo{\n");
        strb.append("  box-shadow: 0 2px 4px rgba(0,0,0,0.25);\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append(".el-sub-menu__title{\n");
        strb.append("  font-size: 16px;\n");
        strb.append("}\n");
        strb.append("\n");
        strb.append("</style>\n");
        File outputFile = new File(DEST_PATH + "/" + FILE_INDEX_NAME);
        if (!outputFile.exists()) {
            outputFile.getParentFile().mkdirs();
            outputFile.createNewFile();
        }
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(strb.toString());
        }
        System.out.println("已生成文件：" + outputFile.getAbsolutePath());


    }

    private static final String FILE_LOGIN_NAME = "Login.vue";

    public static void generateLogin() throws IOException {
        File srcFile = new File(Easy.PROJECT_ABSOLUTE_URL + Easy.SRC_DIR + Easy.convertPackagePath(PACKAGE_NAME) + "/" + FILE_LOGIN_NAME);
        File destFile = new File(DEST_PATH + "/" + FILE_LOGIN_NAME);
        Easy.mkdirs(DEST_PATH);
        Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        System.out.println("已生成文件：" + destFile.getAbsolutePath());
    }

    public static void generate() throws Exception {
        generateIndex();
        generateLogin();
    }
}
